package com.yangyang.androidconnect.controller;

import android.content.Context;

import com.yangyang.androidconnect.common.ApiResult;
import com.yangyang.androidconnect.service.FileService;
import com.yangyang.androidconnect.util.CacheUtils;
import com.yangyang.androidconnect.util.ResponseFileUtil;
import com.yangyang.androidconnect.util.ThumbnailUtil;
import com.yanzhenjie.andserver.annotation.DeleteMapping;
import com.yanzhenjie.andserver.annotation.GetMapping;
import com.yanzhenjie.andserver.annotation.RequestMapping;
import com.yanzhenjie.andserver.annotation.RequestParam;
import com.yanzhenjie.andserver.annotation.RestController;
import com.yanzhenjie.andserver.http.HttpRequest;
import com.yanzhenjie.andserver.http.HttpResponse;
import com.yanzhenjie.andserver.http.ResponseBody;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;

import java.io.File;

@RestController
@RequestMapping(path = "/api/file/v2")
public class FileInfoControllerV2 {

    @GetMapping(path = "/file-db-list")
    public ApiResult fileDbList(
            @RequestParam(value = "path", required = false, defaultValue = "") String path,
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", required = false, defaultValue = "200") Integer pageSize,
            @RequestParam(value = "showHidden", required = false, defaultValue = "false") boolean showHidden
            , Context context
    ) throws Exception {
        path = FilenameUtils.normalize(path);
        String cachePath = CacheUtils.getCachePath(context);
        showHidden = path.contains(cachePath);

        return FileService.getDBList(path, page, pageSize, showHidden);
    }


    @GetMapping(path = "/file-list")
    public ApiResult fileList(
            @RequestParam(value = "path", required = false, defaultValue = "") String path,
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", required = false, defaultValue = "200") Integer pageSize,
            @RequestParam(value = "showHidden", required = false, defaultValue = "false") boolean showHidden
            , Context context
    ) throws Exception {
        path = FilenameUtils.normalize(path);
        String cachePath = CacheUtils.getCachePath(context);
        showHidden = path.contains(cachePath);
        ApiResult apiResult = ResponseFileUtil.getFileListResponse(path, page, pageSize, showHidden);
        return apiResult;
    }

    @GetMapping(path = "/photo-album")
    public ApiResult photoAlbumList(
            @RequestParam(value = "path", required = false, defaultValue = "") String path,
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", required = false, defaultValue = "200") Integer pageSize,
            @RequestParam(value = "showHidden", required = false, defaultValue = "false") boolean showHidden
    ) throws Exception {
        ApiResult apiResult = ResponseFileUtil.getPhotoAlbumListResponse(page, pageSize, showHidden);
        return apiResult;
    }

    @GetMapping(path = "/clean-cache")
    public ApiResult cleanCache(Context context) throws Exception {
        String cachePath = CacheUtils.getCachePath(context);
        File file = FileUtils.getFile(cachePath);
        FileUtils.deleteDirectory(file);
        return ApiResult.ok("缓存已删除");
    }

    @GetMapping(path = "/thumbnail")
    public ResponseBody thumb(@RequestParam("path") String path, HttpResponse response, Context context) throws Exception {
        String outputThumbnailPath = ThumbnailUtil.generateThumbnail(path, context);
        ResponseBody streamBody = ResponseFileUtil.file(outputThumbnailPath, response);
        return streamBody;
    }

    @GetMapping(path = "/get-file")
    public ResponseBody getfile(@RequestParam("path") String path, HttpResponse response) throws Exception {
        ResponseBody streamBody = ResponseFileUtil.file(FilenameUtils.normalize(path), response);
        return streamBody;
    }

    @GetMapping(path = "/stream-file")
    public ResponseBody stream(@RequestParam("path") String path, HttpRequest request, HttpResponse response) throws Exception {
        ResponseBody streamBody = ResponseFileUtil.streamFile(path, request, response);
        return streamBody;
    }

    @DeleteMapping(path = "/delete")
    public ApiResult delete(@RequestParam("path") String path, HttpResponse response) throws Exception {
        return ApiResult.ok("删除功能未开发");
    }

}